﻿using System;
using System.Windows.Forms;

namespace SourceBackup
{
    public partial class OptionsWindow : Form
    {
        public OptionsWindow()
        {
            InitializeComponent();
        }

        private string SOURCEBACKUPTOOLTITLE = "Source Backup";

        private void CheckVSVersion(VSVersion vsVersion, CheckBox chk)
        {
            if (VSExternalTools.IsVSInstalled(vsVersion))
            {
                chk.Checked = VSExternalTools.IsToolRegistered(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, vsVersion);
                if (chk.Checked || !AdminModeTools.IsAdmin())
                    chk.Enabled = false;
            }
            else
                chk.Enabled = false;            
        }

        private void OptionsWindow_Load(object sender, EventArgs e)
        {
            txtDefaultMoveToDir.Text = Properties.Settings.Default.LastMoveToDir;
            CheckVSVersion(VSVersion.VS2008, chkRegisterInVS2008);
            CheckVSVersion(VSVersion.VS2010, chkRegisterInVS2010);
            CheckVSVersion(VSVersion.VS2012, chkRegisterInVS2012);
            CheckVSVersion(VSVersion.VS2013, chkRegisterInVS2013);
            CheckVSVersion(VSVersion.VS2012EFD, chkRegisterInVS2012EFD);
            CheckVSVersion(VSVersion.VS2013EFD, chkRegisterInVS2013EFD);
            CheckVSVersion(VSVersion.VS2015, chkRegisterInVS2015);

            lblAdminModeWarning.Visible = !AdminModeTools.IsAdmin();
        }

        private void RegisterToolForVS(VSVersion vsVersion, CheckBox chk)
        {
            if (chk.Checked)
                VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, vsVersion);
        }
        private void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                RegisterToolForVS(VSVersion.VS2008, chkRegisterInVS2008);
                RegisterToolForVS(VSVersion.VS2010, chkRegisterInVS2010);
                RegisterToolForVS(VSVersion.VS2012, chkRegisterInVS2012);
                RegisterToolForVS(VSVersion.VS2013, chkRegisterInVS2013);
                RegisterToolForVS(VSVersion.VS2015, chkRegisterInVS2015);
                RegisterToolForVS(VSVersion.VS2012EFD, chkRegisterInVS2012EFD);
                RegisterToolForVS(VSVersion.VS2013EFD, chkRegisterInVS2013EFD);

                //if (chkRegisterInVS2008.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2008);
                //if (chkRegisterInVS2010.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2010);
                //if (chkRegisterInVS2012.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2012);
                //if (chkRegisterInVS2013.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2013);
                //if (chkRegisterInVS2012EFD.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2012EFD);
                //if (chkRegisterInVS2013EFD.Checked)
                //    VSExternalTools.SetTool(SOURCEBACKUPTOOLTITLE, Application.ExecutablePath, "$(SolutionDir)", "", 17, VSVersion.VS2013EFD);

                Properties.Settings.Default.LastMoveToDir = txtDefaultMoveToDir.Text;
                Properties.Settings.Default.DisplayExtraFileInfo = checkBoxShowFileInfo.Checked;
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cmdBrowseDefaultMoveTo_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = txtDefaultMoveToDir.Text;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDefaultMoveToDir.Text = fbd.SelectedPath;
            }
        }
    }
}
